package com.hoshiicloud.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.update.UpdateBatchParam;
import com.hoshiicloud.goods.entity.goods.Gallery;
import com.hoshiicloud.goods.param.goods.GalleryQueryParam;
import com.hoshiicloud.goods.service.GalleryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author linjx on 2019/8/12
 */
@RestController
@RequestMapping("/gallery/gallery")
public class GalleryController {

    @Autowired
    private GalleryService galleryService;


    @PostMapping("/queryByParam")
    public Response queryByParam(@RequestBody GalleryQueryParam param) {
        QueryWrapper<Gallery> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(param.getId() != null, Gallery::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getImgUrl()), Gallery::getImgUrl, param.getImgUrl())
                .eq(StringUtils.isNotBlank(param.getImgDesc()), Gallery::getImgDesc, param.getImgDesc())
                .eq(StringUtils.isNotBlank(param.getImgUrl()), Gallery::getImgUrl, param.getImgUrl());
        String key = param.toString();
        return Response.data(galleryService.getGalleryByWrapper(wrapper, key));
    }

    @PostMapping("/deleteBatchs")
    @ApiOperation(value = "根据id集合进行批量删除")
    public Response deleteBatchs(@RequestBody List<Long> ids) {
        return Response.data(galleryService.deleteBatchs(ids));
    }

    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量新增相册信息")
    public Response saveGalleryBatch(@RequestBody List<Gallery> galleries) {
        return Response.data(galleryService.saveGalleryBatch(galleries));
    }

    @PostMapping("/queryBatch")
    @ApiOperation(value = "自定义批量查询相册信息")
    public Response queryBatch(@RequestBody List<OmnipotentWrapper> param) {
        return Response.data(galleryService.queryBatch(param));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据Id删除相册信息")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(galleryService.deleteById(id));
    }

    @PostMapping("/updateBatch")
    @ApiOperation(value = "批量更新")
    public Response updateBatch(@RequestBody OmnipotentParam<Gallery> param) {
        return Response.data(galleryService.updateBatch(param));
    }
}
